/**
 * Cinnamon Web Statistics
 */
var Cinnamon = {
	smell: function() {
		// filter switcher
		$('#filters-switcher, #filters-description').click( function(event) {
			var filters = $('#filters');
			if ( $(filters).is('.edit-mode') ) {
				$('#filters-switcher').html('edit');
				$(filters).removeClass('edit-mode');
			} else {
				$('#filters-switcher').html('cancel');
				$(filters).addClass('edit-mode');
			}
			event.stopPropagation();
		} );
		
		// skip to...
		$('div.container h1').each( function(i) {
			$('#skip-to dd ul').append('<li><a href="#'+ $(this).parent().attr('id') +'" title="Jump to '+ $(this).text() +'">'+ $(this).text() +'</a></li>');
		});
		$('a[@href^="#"]').click( function(){
			$($(this).attr('href')).ScrollTo(300);
			return false;
		} );
		$('#skip-to dt span.ajax').click( function(event) {
			$('#header').ScrollTo(300);	
			event.stopPropagation();
		} );
		
		// widgets
		$('#sidebar dt').click( function() {
			$(this).parent().find('dd').toggle();
		} );
				
		// alternative rows
		$('div.datagrid table.zebra tbody tr:nth-child(odd)').addClass('alt');
		
		// datagrid fragments
		$('div.datagrid > div.content > table.fragment').each( function() {
			var thisChildren = $(this).find('thead tr:first-child').children();
			var firstChildren = $(this).parent().find('table:first-child thead tr:first-child').children();
			
			for ( var i=0; i<thisChildren.length; i++ ) {
				$(thisChildren[i]).css('width', $(firstChildren[i]).css('width'));
			}
		} );
		
		
		// Track downloads
		$('#track_downloads').change ( function() {
		var cd = document.getElementById('track_downloads');
			if (cd.checked) {
				$('#tracked_extensions_div').addClass('visibleBlock');
				$('#tracked_extensions').val("doc,pdf,zip,rar");
			} else {
				$('#tracked_extensions_div').addClass('hidden');
				$('#tracked_extensions').val("");
			}
		});
		
		
	},
	
	//
	// Cookie functions
	//
	setCookie: function(name, value, days) {
		var expires = '';
		if ( days ) {
			var date = new Date();
			date.setTime( date.getTime() + (days*24*60*60*1000) );
			var expires = '; expires='+date.toGMTString();
		}
		document.cookie = name +'='+ value + expires +'; path=/';
	},

	getCookie: function(name) {
		var nameEQ = name + '=';
		var ca = document.cookie.split(';');
		for( var i=0; i<ca.length; i++ ) {
			var c = ca[i];
			while ( c.charAt(0) == ' ' ) {
				c = c.substring(1,c.length);
			}
			if ( c.indexOf(nameEQ) == 0 ) {
				return c.substring(nameEQ.length,c.length);
			}
		}
		return null;
	}
};

$(document).ready( function() { Cinnamon.smell(); } );